package com.moto.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.imaging.ImageInfo;
import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.Imaging;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.moto.entity.MotoStartPic;
import com.moto.service.AppConfigService;
import com.moto.util.CreateID;
import com.moto.util.DateUtils;
import com.moto.util.QiNiuFileUpload;

@Scope("prototype")
@RequestMapping("/config")
@Controller
public class AppConfigController {
	/**
	 * 日志记录
	 */
	private static final Logger LOG = LoggerFactory.getLogger(AppConfigController.class);

	@Autowired
	private AppConfigService appConfigService;

	/**
	 * 获取所有启动图片
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/start", method = RequestMethod.GET)
	public String startPicConfig(Model model) {
		List<MotoStartPic> list = appConfigService.getStartPicList();
		LOG.debug("获取所有启动图片");
		model.addAttribute("list", list);
		return "start";
	}

	/**
	 * 删除图片.GET方法，可用ajax调用
	 * 
	 * @param picId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/start/delete", method = RequestMethod.GET)
	public Map<String, String> deleteStartPic(@RequestParam("picId") String picId) {
		Map<String, String> map = Maps.newHashMap();
		boolean flag = appConfigService.deleteStartPic(picId);
		if (flag) {
			map.put("flag", Boolean.TRUE.toString());
		} else {
			map.put("flag", Boolean.FALSE.toString());
		}
		return map;
	}

	/**
	 * 上传启动图片
	 * 
	 * @return
	 */
	@RequestMapping(value = "/start/upload", method = RequestMethod.POST)
	public String uploadStartPic(MotoStartPic motoStartPic, @RequestParam MultipartFile start_pic,
			HttpServletRequest request) {

		String picId = CreateID.generate();

		/**
		 * 处理图片上传
		 */
		// 如果想上传多个文件，那么这里就要用MultipartFile[]类型来接收文件，并且还要指定@RequestParam注解
		// 并且上传多个文件时，前台表单中的所有<input type="file"/>的name都应该是myfiles，否则参数里的myfiles无法获取到所有上传的文件
		try {
			String type = start_pic.getContentType();
			String fileType = type.substring(type.indexOf("/") + 1, type.length());
			String fileName = picId + "_" + DateUtils.dateToUnixTimestamp() + "." + fileType;
			//String realPath = "/opt/tomcat/tomcat-moto/upload/" + fileName;
			String realPath = "D:/upload/" + fileName;
			FileUtils.copyInputStreamToFile(start_pic.getInputStream(), new File(realPath));
			File file = new File(realPath);
			ImageInfo imageInfo = Imaging.getImageInfo(file);
			//TODO @阿飞 图片上传文件格式不对，获取文件后缀名成了octet-stream
			//TODO 程序执行到此处，会报异常。picId有值，但是motoStartPic.setPicId(picId);执行不成功
			motoStartPic.setPicOrder("1");
			motoStartPic.setPicId(picId);
			Map<String,Integer> map = Maps.newConcurrentMap();
			map.put("width", imageInfo.getWidth());
			map.put("height", imageInfo.getHeight());
			Gson gson = new Gson();
			motoStartPic.setPicSize(gson.toJson(map));
			String url = QiNiuFileUpload.upload(realPath, fileName);
			motoStartPic.setPicUrl(url);
		} catch (ImageReadException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		int i = appConfigService.saveStartPic(motoStartPic);
		if (i == 1) {
			LOG.info("上传图片成功");
		} else {
			LOG.info("上传图片失败");
		}

		return "redirect:/index.jsp";
	}
}
